/* 
 * DBFactory.java 01.10.2011
 *  
 *  
 * Candidate: Alessandra Otterbach
 * Prometric ID: Algisina1
 * Username: Algisina
 *  
 * Sun Certified Developer for Java 2 Platform, Standard Edition Programming 
 * Assignment (CX-310-252A)
 *  
 * This class is part of the Programming Assignment of the Sun Certified 
 * Developer for Java 2 Platform, Standard Edition certification program, must 
 * not be used out of this context and must be used exclusively by Sun 
 * Microsystems, Inc. 
 */
package suncertify.db;

import java.io.IOException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import suncertify.db.exc.RecordNotFoundException;
import suncertify.rm.UrlyBirdDataFactory;
import suncertify.rm.UrlyBirdDataRemote;

/**
 * Returns different types of database: standalone or rmi. TODO Aufteilen?
 * 
 * @author Alessandra Otterbach
 * 
 */
public class DBFactory {

    /**
     * Static method that gets a database handle. The DBClient is a local
     * object.
     * 
     * @param dbLocation
     *            the path to the database on disk.
     * @return A <code>DBClient</code> instance.
     * @throws IOException
     *             Thrown if an <code>IOException</code> is encountered in the
     *             <code>DvdDatabase</code> class. <br>
     *             For more information, see {@link DvdDatabase}.
     * @throws ClassNotFoundException
     *             Thrown if an <code>ClassNotFoundException</code> is
     *             encountered in the <code>DvdDatabase</code> class. <br>
     *             For more information, see {@link DvdDatabase}.
     * @throws RecordNotFoundException
     */
    public static DB getLocal(String dbLocation)
	    throws RecordNotFoundException, IOException {
	DB data = Data.getInstance(dbLocation);
	return data;
    }

    /**
     * Static method that creates an RMI connection. The DBClient is a remote
     * object.
     * 
     * @param hostname
     *            The ip or address of the host machine.
     * @param port
     *            the port the RMI Registry is listening on.
     * @return A DBClient instance.
     * @throws RemoteException
     *             Indicates that a remote instance of the DBClient interface
     *             cannot be created.
     */
    public static UrlyBirdDataRemote getRemote(String hostname, String port)
	    throws RemoteException {
	String url = "rmi://" + hostname + ":" + port + "/URLyBird";

	try {
	    UrlyBirdDataFactory factory = (UrlyBirdDataFactory) Naming
		    .lookup(url);
	    return (UrlyBirdDataRemote) factory.getUBData();
	} catch (NotBoundException e) {
	    System.err
		    .println("URLyBird not registered: " + e.getMessage());
	    throw new RemoteException("URLyBird not registered: ", e);
	} catch (java.net.MalformedURLException e) {
	    System.err.println(hostname + " not valid: " + e.getMessage());
	    throw new RemoteException("cannot connect to " + hostname, e);
	}
    }

}
